﻿using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;

namespace HappyWorks.Pattern.Creational.FactoryMethod
{
    public interface IProduct
    { 
        
    }
    public class ProductA : IProduct
    { }
    public class ProductB : IProduct
    { }

    public interface IFactory
    {
        IProduct Create();
        IProduct Create(string name);
    }



    public  class ProductRegist
    { 
        const string sectionName="ProductCollection";
        NameValueCollection collection =ConfigurationManager.GetSection(sectionName) as NameValueCollection;
        public Type this[string name]
        {
            get { return Type.GetType(collection[name]); } 
        }
        
    }

    public class Factory : IFactory
    {
        const string defalutName = "Default";
        public readonly ProductRegist regist = new ProductRegist();
        public IProduct Create()
        {           
            return Activator.CreateInstance(regist[defalutName]) as IProduct;
        }
        public IProduct Create(string name)
        {
            return Activator.CreateInstance(regist[name]) as IProduct;
        }
    }
   
}
